// 生成器：返回一个迭代器
function * generator (arr) {
  for (let v of arr) {
    yield v
  }
}
// const iterator = generator([1, 2, 3])
// console.log(iterator.next())
// console.log(iterator.next())
// console.log(iterator.next())
// console.log(iterator.next())

// 自己实现
function myGenerator(arr) {
  let nextIndex = 0
  return {
    next () {
      return nextIndex < arr.length ? { value: arr[nextIndex ++], done: false } : { value: undefined, done: true }
    }
  }
}



const o = {
  0: 1,
  1: 2,
  2: 3,
  length: 3
}
Object.prototype[Symbol.iterator] = iterator
function iterator () {
  let index = 0
  let _this = this
  return {
    next() {
      return index < _this.length ? { value: _this[index ++], done: false } : { value: undefined, done: true }
    }
  }
}
for (let v of o) {
  console.log(v)
}
